import { Controller, Get, Post, Body, Put, Param, Query, Delete, HttpCode } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBody, ApiConsumes, ApiQuery, ApiBearerAuth, ApiParam } from '@nestjs/swagger';
import { DeptService } from './dept.service';
import { CreateDeptDto, UpdateDeptDto, ListDeptDto } from './dto/index';
import { RequirePermission } from 'src/common/decorators/require-premission.decorator';
import { RepeatSubmit } from 'src/common/decorators/repeat-submit.decorator';
import { Operlog } from 'src/common/decorators/operlog.decorator';
import { BusinessType } from 'src/common/constant/business.constant';
import { UserTool, UserToolType } from '../user/user.decorator';

@ApiTags('部门管理')
@Controller('system/dept')
export class DeptController {
  constructor(private readonly deptService: DeptService) {}

  @ApiOperation({ summary: '部门管理创建' })
  @ApiBody({ type: CreateDeptDto, required: true })
  @RequirePermission('system:dept:add')
  @Post()
  @RepeatSubmit()
  @HttpCode(200)
  @Operlog({ businessType: BusinessType.INSERT })
  create(@Body() createDeptDto: CreateDeptDto, @UserTool() { injectCreate }: UserToolType) {
    return this.deptService.create(injectCreate(createDeptDto));
  }

  @ApiOperation({ summary: '部门管理列表' })
  @RequirePermission('system:dept:list')
  @ApiQuery({ type: ListDeptDto })
  @Get('/list')
  findAll(@Query() query: ListDeptDto) {
    return this.deptService.findAll(query);
  }

  @ApiOperation({ summary: '部门管理详情' })
  @RequirePermission('system:dept:query')
  @Get(':id')
  @ApiParam({ name: 'id', required: true, description: '部门id' })
  findOne(@Param('id') id: string) {
    return this.deptService.findOne(+id);
  }

  @ApiOperation({ summary: '部门管理黑名单' })
  @RequirePermission('system:dept:query')
  @Get('/list/exclude/:id')
  @ApiParam({ name: 'id', required: true, description: '部门id' })
  findListExclude(@Param('id') id: string) {
    return this.deptService.findListExclude(+id);
  }

  @ApiOperation({ summary: '部门管理更新' })
  @ApiBody({ type: UpdateDeptDto, required: true })
  @RequirePermission('system:dept:edit')
  @RepeatSubmit()
  @Put()
  @Operlog({ businessType: BusinessType.UPDATE })
  update(@Body() updateDeptDto: UpdateDeptDto, @UserTool() { injectUpdate }: UserToolType) {
    return this.deptService.update(injectUpdate(updateDeptDto));
  }

  @ApiOperation({ summary: '部门管理删除' })
  @RequirePermission('system:dept:remove')
  @Delete(':id')
  @ApiParam({ name: 'id', required: true, description: '部门id' })
  @Operlog({ businessType: BusinessType.DELETE })
  remove(@Param('id') id: string) {
    return this.deptService.remove(+id);
  }
}
